// pages/points/pointsapplication/pointsapplication.js
const wxapi = require('../../utils/wxapi.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    depArray: [],
    depIndex: 0,
    depValue: 0,
    whoIndex: 0,
    typeArray: [
      {
        name:'规章',
        id:1
      },
      {
        name:'其他文章',
        id:2
      }
    ],
    typeIndex: 0,
    typeValue: 1,
    context: '',
    title: '',
    imgId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let sec = wx.getStorageSync("secKey")
    this.getDepData(sec);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindDepPickerChange: function (e) {
    this.setData({
      depIndex: e.detail.value
    })
  },
  bindWhoPickerChange: function (e) {
    this.setData({
      whoIndex: e.detail.value
    })
  },
  bindTypePickerChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  getDepData(sec) {
    let that = this;
    wxapi.getTypes(sec,1).then(res => {
      that.setData({
        depArray: res.data
      })
    })
  },
  bindSubmit(e) {
    console.log(e)
    let data = e.detail.value;
    if (data.title == "") {
      wx.showModal({
        title: '注意!',
        content: '请输入文章标题',
        showCancel: false
      })
      return;
    }
    if (data.context == "") {
      wx.showModal({
        title: '注意!',
        content: '请输入正文',
        showCancel: false
      })
      return;
    }
    data.sercet = wx.getStorageSync('secKey');
    data.imgId = this.data.imgId;
    wxapi.submitPointsForm(data).then(res => {
      if (res.suc) {
        // wx.switchTab({
        //   url: '/pages/points/points',
        // })
      }
    }).catch(err => {
      wx.showModal({
        title: '错误',
        content: err,
      })
    })
  },
  uploadImg(){
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: '',
          filePath: tempFilePaths,
          name: 'imgsrc',
          success:function(res){
            console.log(res.data.data.imgId)
            that.setData({
              imgId:res.data.data.imgId
            })
          },
          fail:function(){
            wx.showModal({
              title: '错误',
              content: '上传失败了!请重试',
            })
          }
        })
      }
    })
  }


})